package managers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
/*将assets文件夹下的数据库写入SD卡中
 * @author Dave */
public class WriteToSD {
 private Context context;
 String assetsName;
 String filePath;


 public WriteToSD(Context context,String assetsName,String filePath){
 this.context = context;
 this.assetsName = assetsName;
  this.filePath = filePath;
 }

 public void write2()
 {
  if(!isExist()){
   write();
  }
 }
 private void write(){
 InputStream inputStream;
 try {
  inputStream = context.getResources().getAssets().open(assetsName);
  File file = new File(filePath);
  if(!file.exists()){
  file.mkdirs();
  }
  FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/" + assetsName);
  byte[] buffer = new byte[512];
  int count = 0;
  while((count = inputStream.read(buffer)) > 0){
  fileOutputStream.write(buffer, 0 ,count);
  }
  fileOutputStream.flush();
  fileOutputStream.close();
  inputStream.close();
  System.out.println("success");
 } catch (IOException e) {
  e.printStackTrace();
 }
 }
 private boolean isExist(){
 File file = new File(filePath + "/" + assetsName);
 if(file.exists()){
  return true;
 }else{
  return false;
 }
 }
}